package com.titan.demo.camel.sample.Main方法;

import org.apache.camel.CamelContext;
import org.apache.camel.Component;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.direct.DirectComponent;
import org.apache.camel.impl.DefaultCamelContext;

import com.titan.demo.camel.utils.CamelUtils;

public class RunMain {

	public static void main(String[] args) {
		try {
			run();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	
	private static void run() throws Exception {
        CamelContext context = null;
        try {
        	// 创建上下文
            context = new DefaultCamelContext();
            
            // 添加组件（该步骤可以忽略）
            // 备注：context如果找不到对应的component会自动去META-INF\services\org\apache\camel\component查找对应的component并自动加载.所以常用组件可省略
            Component com = new DirectComponent();
            context.addComponent("direct", com);

            // 创建Endpoint（该步骤可以忽略）
            com.createEndpoint("direct:start");
            
            // 定义路由
            RouteBuilder builder = new RouteBuilder() {
                public void configure() throws Exception {
                    from("direct:start").log("${body}").to("mock:end");
                }
            };

            // 把路由加入context中
            context.addRoutes(builder);
            
            // 启动context，调用CamelContext的start() 方法，这样可以启动Camel内部有关消息发送，接收，处理所使用的线程。
            context.start();
        } finally {
            CamelUtils.close(context);
        }		
	}
}
